<template>
	<div :class="className">
		<Chart :cdata="cdata" />
	</div>
</template>

<script>
import Chart from './chart';

export default {
    components: {
        Chart,
    },
    props: {
        className: { type: String, default: '' },
        isMock: { type: Boolean, default: false },
    },
    data() {
        return {
            dataTimer: null,
            cdata: [],
        };
    },
    mounted() {
        if (this.isMock) {
            this.mock();
            return;
        }

        this.getData();
        this.setTimer();
    },
    beforeUnmount() {
        if (this.dataTimer) {
            clearInterval(this.dataTimer);
            this.dataTimer = null;
        }
    },
    methods: {
        setTimer() {
            this.dataTimer = setInterval(async () => {
                await this.getData();
            }, 60000);  // 60s
        },
        mock() {
            this.cdata = [
                { equipCode: 'OP010', performanceRate: 0.221 },
                { equipCode: 'OP020', performanceRate: 0.312 },
                { equipCode: 'OP030', performanceRate: 0.343 },
                { equipCode: 'OP040-1', performanceRate: 0.344 },
                { equipCode: 'OP040-2', performanceRate: 0.455 },
                { equipCode: 'OP050', performanceRate: 0.354 },
                { equipCode: 'OP060', performanceRate: 0.565 },
                { equipCode: 'OP070', performanceRate: 0.126 },
                { equipCode: 'OP080', performanceRate: 0.787 },
                { equipCode: 'OP090', performanceRate: 0.348 },
                { equipCode: 'OP100', performanceRate: 0.689 },
                { equipCode: 'OP110-1', performanceRate: 0.895 },
                { equipCode: 'OP110-2', performanceRate: 0.676 },
                { equipCode: 'OP120', performanceRate: 0.784 },
                { equipCode: 'OP130', performanceRate: 0.653 },
                { equipCode: 'OP140', performanceRate: 0.682 },
                { equipCode: 'OP150-1', performanceRate: 0.762 },
                { equipCode: 'OP150-2', performanceRate: 0.563 },
                { equipCode: 'OP160', performanceRate: 0.784 },
                { equipCode: 'OP170', performanceRate: 0.785 },
                { equipCode: 'OP180', performanceRate: 0.786 },
                { equipCode: 'OP200', performanceRate: 0.657 },
                { equipCode: 'OP210', performanceRate: 0.345 },
                { equipCode: 'OP220', performanceRate: 0.234 },
                { equipCode: 'OP230', performanceRate: 0.467 },
                { equipCode: 'OP240', performanceRate: 0.876 },
                { equipCode: 'OP250', performanceRate: 0.342 },
                { equipCode: 'OP260', performanceRate: 0.456 },
                { equipCode: 'OP270', performanceRate: 0.689 },
                { equipCode: 'OP280', performanceRate: 0.543 },
                { equipCode: 'OP290', performanceRate: 0.986 },
            ]
        },
        async getData() {
            try {
                const resp = await this.$API.reportequipstat.oee.get();
                if (resp.code === 200) {
                    this.cdata = (resp.data[0]?.oeeList ?? []).reverse(); // 数据倒序
                }
            } catch {
                console.warn("[equep-oee] 无响应数据");
            }
        }
    }
}
</script>
